package 爆刷leetcode.balabala;

/**
 * @Author yaya
 * @Create 2024/5/20 20:37
 * Description:
 */
public class MyArray {
    private int size;
    private int[] array;

    public MyArray(){
        this(5);
    }

    public MyArray(int len){
        array = new int[len];
        size = 0;
    }

    public boolean isFull(){
        return this.size == array.length + 1;
    }

    public boolean isEmpty(){
        return this.size == 0;
    }


    // 增删改查
    public void add(int index, int num){
        if (isFull()) throw new RuntimeException("满，不能添加");;
        if (index < 0 || index > size) throw new RuntimeException("越界");

        // 往后移动数据
        for (int i = size - 1 ; i > index ; i--) {
            array[i + 1] = array[ i ];
        }
        array[index] = num;
        size++;
    }
    public void addFirst(int num){
        add(0,num);
    }

    public void addLast(int num){
        add(size, num);
    }

    // 删
    public void delFirst(){
        del(0);
    }

    public void del(int index){
        if (isEmpty()) throw new RuntimeException("空，不能删除");
        if (index < 0 || index > size) throw new RuntimeException("越界");

        int result = array[index];

        // 这里的判断条件一定是不能包括边界的，否则会超出边界
        for (int i = index; i < size - 1; i++) {
            array[i ] = array[i + 1];
        }
        size--;

    }

    public void delLast(){
        del(size - 1);
    }


    // 改
    public int update(int index, int num) {
        if (index < 0 || index > size) throw new RuntimeException("越界");
        return array[index] = num;
    }

    // 查
    public int get(int index){
        if (index < 0 || index > size) throw new RuntimeException("越界");
        return array[index];
    }

    // 含
    public boolean contain(int num){
        for (int i = 0; i < size; i++) {
            if (num == array[i]) return true;
        }

        return false;
    }

    // 扩缩正反印

    public void print(){
        for (int i = 0; i < size; i++) {
            System.out.print(array[i]);
        }
    }

    public void reversePrint(){
        for (int i = size - 1; i >= 0; i--) {
            System.out.print(array[i]);
        }
    }


    public static void main(String[] args) {
        MyArray myArray = new MyArray();
        myArray.addFirst(1);
        myArray.add(1,2);
        myArray.add(2,4);
        myArray.addLast(5);
        myArray.addLast(9);


        myArray.del(4);

        myArray.print();
        myArray.reversePrint();
    }

}
